<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Bargain;
use App\Models\StoreGood;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class BargainController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Bargain(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('uid');
            $grid->column('product_id');
            $grid->column('stock');
            $grid->column('start_time');
            $grid->column('stop_time');
            $grid->column('min_price');
            $grid->column('bargain_max_price');
            $grid->column('bargain_min_price');
            $grid->column('status');
            $grid->column('price');
            $grid->column('goods_name');
            $grid->column('goods_image')->image('',100,100);
            $grid->column('haggle_frequency');
            $grid->column('partake');
            $grid->column('haggle_success');
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Bargain(), function (Show $show) {
            $show->field('id');
            $show->field('uid');
            $show->field('product_id');
            $show->field('stock');
            $show->field('start_time');
            $show->field('stop_time');
            $show->field('min_price');
            $show->field('bargain_max_price');
            $show->field('bargain_min_price');
            $show->field('status');
            $show->field('price');
            $show->field('goods_name');
            $show->field('goods_image');
            $show->field('haggle_frequency');
            $show->field('partake');
            $show->field('haggle_success');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Bargain(), function (Form $form) {
            $form->display('id');
            $form->text('uid');
//            $form->text('product_id');
            $form->selectTable('product_id', '商品id')
                ->from(UserTable::make())
                ->model(StoreGood::class, 'id', 'store_name','stock','price');
            $form->text('stock');
            $form->text('start_time');
            $form->text('stop_time');
            $form->text('min_price');
            $form->text('bargain_max_price');
            $form->text('bargain_min_price');
            $form->text('status');
            $form->text('price');
//            $form->text('goods_name');
            $form->text('goods_name');
            $form->image('goods_image')->disk('qiniu')->saveFullUrl('http://ocdn.xiaxuele.top/');
            $form->text('haggle_frequency');
            $form->text('partake');
            $form->text('haggle_success');

            $form->display('created_at');
            $form->display('updated_at');
        });
    }


}
